// 网格信息展示
let WGlabelArr = [];
let dzdrawTool = undefined;
import areaJSON from "@/assets/json/area.json";
function init(callabck) {
  if (!dzdrawTool) {
    dzdrawTool = new vis3d.plot.Tool(viewer, {
      canEdit: false,
    });
  }
  let features = areaJSON.features;
  let colors = [
    "#FFFF00",
    "#FF66FF",
    "#66FF33",
    "#6666FF",
    "#660099",
    "#999933",
  ];
  for (let i = 0; i < features.length; i++) {
    let feature = features[i];
    let properties = feature.properties;
    let O_Name = properties.O_Name;
    let attr = properties.attr;
    attr.O_Name = O_Name;
    attr.XQ_Name = properties.XQ_Name;
    let lnglats = feature.geometry.coordinates[0][0];
    let center = getCenterByPositions(lnglats); // 计算面中心点
    console.log("lnglats===>", lnglats);
    let p = Cesium.Cartesian3.fromRadians(
      center.longitude,
      center.latitude,
      1200
    );
    let WGlabel = addLabel(p, O_Name, Cesium.Color.RED, 2000, Number.MAX_VALUE);
    WGlabel.dataType = "WGlabel";
    WGlabel.attr = attr;
    WGlabelArr.push(WGlabel);
    dzdrawTool.createByPositions({
      type: "polygon",
      style: {
        heightReference: 1,
        color: colors[i],
        colorAlpha: 0.5,
      },
      positions: lnglats,
      success: function (entObj, ent) {
        entObj.O_Name = O_Name;
        ent.attr = attr;
        ent.dataType = "area";
        ent.click = (evt, ent) => {
          callabck(ent.attr);
        };
      },
    });
  }

  // 添加面的高度监听
  viewer.scene.postRender.addEventListener(function () {
    var cameraP = viewer.camera.position;
    const ctgc = Cesium.Cartographic.fromCartesian(cameraP.clone());
    if (ctgc.height > 4000) {
      // 显示面
      setPolygonsVisible(true);
    } else {
      setPolygonsVisible(false);
    }
  }, this);
}

function setPolygonsVisible(visible) {
  if (!dzdrawTool) return;
  const entityObjArr = dzdrawTool.entityObjArr;
  for (let i = 0; i < entityObjArr.length; i++) {
    const entObj = entityObjArr[i];
    entObj.setVisible(visible);
  }
}

function getCenterByPositions(lnglats) {
  if (!lnglats || lnglats.length < 3) {
    console.warn("传入数组长度不够！");
    return;
  }
  let ps = [];
  for (let i = 0; i < lnglats.length; i++) {
    let lnglat = lnglats[i];
    let point = turf.point([lnglat[0], lnglat[1]]);
    ps.push(point);
  }
  let features = turf.featureCollection(ps);
  let center = turf.center(features).geometry.coordinates;
  return Cesium.Cartographic.fromDegrees(center[0], center[1]);
}

function addLabel(p, text, color, minH, maxH) {
  return viewer.entities.add({
    position: p,
    label: {
      text: text,
      font: "14pt Source Han Sans CN", //字体样式
      fillColor: color || Cesium.Color.BLACK, //字体颜色
      backgroundColor: Cesium.Color.SNOW.withAlpha(0.5), //背景颜色
      showBackground: true, //是否显示背景颜色
      style: Cesium.LabelStyle.FILL, //label样式
      outlineWidth: 2,
      // heightReference: 1,
      distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
        minH || 10,
        maxH || 2000
      ),
      pixelOffset: new Cesium.Cartesian2(10, 0), //偏移
    },
  });
}

function destroy() {
  if (dzdrawTool) {
    dzdrawTool.destroy();
    dzdrawTool = undefined;
  }

  WGlabelArr.forEach((label) => {
    viewer.entities.remove(label);
  });
  WGlabelArr = [];
}

export { init, destroy };
